var TOWER_BUFFER=[];
var Tower = cc.Sprite.extend({
    _bufferIndex : -1,
    _life : true,
    _series : 0,
    _power : 0,
    _speed : 0,
    _fireRange : 0,
    _fireTime : 0,
    _canFire : true,
    _bulletRes : "",
    _bulletBuffer : [],
    _locationArray : [],// [obj][x][y]
    ///////////////////////////////////////////////////////////
    ctor : function( filename,towerData ){//series,power,speed,fireRange,fireTime,bulletRes
        this._super(filename);
        this._series = towerData.series;
        this._power = towerData.pw;
        this._speed = towerData.sp;
        this._fireRange = towerData.fr;
        this._fireTime = towerData.ft;
        this._bulletRes = towerData.bRes;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._super();
    },
    ////////////////////////// moving //////////////////////////
    getBulletFromBuffer : function(){
        for (var i=0;i<this._bulletBuffer.length;++i){
            var bullet = this._bulletBuffer[i];
            if(!bullet.isVisible()){
                return bullet;
            }
        }
        return null;
    },
    fighting : function(direct){
        if (this._canFire){
            var that=this;
            var distance = cc.pDistance(this._position,direct );
            if ( distance<this._fireRange ){
                cc.log("distance: "+distance);
                this._canFire = false;

                var bullet = this.getBulletFromBuffer();
                if ( bullet ){
                    cc.log("get bullet from buffer");
                    bullet.setPosition(this.getPosition());
                    bullet.setSpeedScale(direct,distance);
                    // set bullet size

                    bullet.setVisible(true);
                }else{
                    cc.log("new bullet");
                    var name = "res/"+this._bulletRes;
                    var newBullet = Bullet.create( name,this._power,this._speed );
                    newBullet.setPosition(this.getPosition());
                    newBullet.setSpeedScale(direct,distance);
                    // set bullet size

                    var mainLayer = getMainLayerInstance();
                    mainLayer.addChild(newBullet,10 );
                    this._bulletBuffer.push(newBullet);
                }
                this.runAction( cc.Sequence.create(
                    new cc.DelayTime( this._fireTime ),
                    new cc.CallFunc( function(){ that._canFire = true;cc.log("open fire trigger");} )
                ));
            }
        }
    },
    setIndex : function(index){
        this._bufferIndex=index;
    },
    getIndex : function(){
        return this._bufferIndex;
    },
    removeMe : function(){
        this._life = false;
        this.removeFromParent(true);
        TOWER_BUFFER.splice(this._bufferIndex,1);
        for( var i=this._bufferIndex;i<TOWER_BUFFER.length;i++){
            var tower=TOWER_BUFFER[i];
            if(tower){
                var index = tower.getIndex();
                tower.setIndex( index );
            }
        }
    }
});
////////////////////////////////////////////////////////////////
Tower.create = function (filename,towerData ) {
    var tower = new Tower(filename,towerData );
    //put  into our container
    tower.setIndex( TOWER_BUFFER.length );
    TOWER_BUFFER.push( tower );
    return tower;
};
